/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rapidp2p.backend;
//import the RequestHandler here, then use its getter method to get file name chosen

import java.io.*;
import java.net.*;
import rapidp2p.backend.RequestHandler_1;

/**
 *
 * @author LF
 */
class TCPServer {

    ServerSocket welcomeSocket = null;
    Socket connectionSocket = null;
    //String wantedFile = null;
    
    //TCPServer (){
        //this.wantedFile = wantedFile;
    //}

    public void listenSocket() {
        String file = RequestHandler_1.filePath;
        try {
            welcomeSocket = new ServerSocket(3248);
        } catch (IOException ex) {
            System.out.println("Could not listen on port 3248");
            System.exit(-1);
        }

        while (true) {
            ClientWorker w;
            try {
                w = new ClientWorker(welcomeSocket.accept(), file);
                Thread t = new Thread(w);
                t.start();
                //connectionSocket = welcomeSocket.accept();
                //outToClient = new BufferedOutputStream(connectionSocket.getOutputStream());
            } catch (IOException ex) {
                System.out.println("Accept failed: 3248\n");
                System.exit(-1);
            }

        }
    }

    public static void main(String[] args) {
        InetAddress ipServer = null;
        try {
            ipServer = InetAddress.getByName("localhost");
        } catch (UnknownHostException uhe) {
            System.out.println("Unkknown host");
        }
        System.out.println("Server IP : " + ipServer.toString());
        TCPServer tcpserv = new TCPServer();
        tcpserv.listenSocket();
    }
}
